package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
	"fmt"

	log "github.com/sirupsen/logrus"
)

// 获取活动排名 15004
func (p *Player) GetActivityRankRequest(req *sp.GetActivityRankRequest) (res *sp.GetActivityRankResponse) {
	us := p.User
	arG := us.GetActRankGroup()
	if arG == nil {
		err := errors.NewErrcode(data.Errcode.NotFound, fmt.Sprintf("没找到用户分组:%d", us.UserGroup))
		res = &sp.GetActivityRankResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 重新更新玩家排名
	userProgressData := us.GetUserActProgress()
	actProgress, _ := userProgressData.GetActivityProgress(arG.ActId)
	if val, ok := arG.UserActRankGroup[us.Uid.Hex()]; ok {
		actProgress.RankId = val.RankId
	}
	userProgressData.ActivityProgressUpsetAndUpdate(actProgress)

	res = &sp.GetActivityRankResponse{
		ActivID:      arG.ActId,
		RankRewardId: arG.RewardId,
		List:         lib.FormatActRankLst(arG.UserActRankGroup),
	}
	return
}

// 获取活动旧的排名 15006
func (p *Player) GetOldActivityRankRequest(req *sp.GetOldActivityRankRequest) (res *sp.GetOldActivityRankResponse) {
	us := p.User
	// 获取用户旧的排名分组id
	oldUserActRank, err := m.GetOldUserActRank(us.Uid.Hex())
	if err != nil {
		log.WithFields(log.Fields{
			"method": "GetOldActivityRankRequest",
			"uid":    us.Uid.Hex(),
			"err":    err,
		}).Error("获取用户旧的排名分组失败")
		res = &sp.GetOldActivityRankResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 当天时间
	nowUnix := utils.TNow().Unix()
	// 当天和记录的活动奖励时间是否一样
	if !utils.IsSameDay(nowUnix, oldUserActRank.RecordTime) {
		log.WithFields(log.Fields{
			"method":     "GetOldActivityRankRequest",
			"uid":        us.Uid.Hex(),
			"nowUnix":    nowUnix,
			"RecordTime": oldUserActRank.RecordTime,
		}).Error("旧的排名和今天时间不是同一天")
		res = &sp.GetOldActivityRankResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}
	// 获取旧的排名
	oldActRankGroup, err := m.GetOldActRankGroup(oldUserActRank.GroupId)
	if err != nil {
		log.WithFields(log.Fields{
			"method":  "GetOldActivityRankRequest",
			"uid":     us.Uid.Hex(),
			"GroupId": oldUserActRank.GroupId,
			"err":     err,
		}).Error("获取旧的排名分组失败")
		res = &sp.GetOldActivityRankResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.GetOldActivityRankResponse{
		State:        oldUserActRank.State,
		ActivID:      oldActRankGroup.ActId,
		RankRewardId: oldActRankGroup.RewardId,
		List:         lib.FormatActRankLst(oldActRankGroup.UserActRankGroup),
	}
	return
}

// 旧活动的排名活动推送 s-c 15007
func (p *Player) OldActivityRankNotice() {

}

// 领取旧的活动排名奖励 15008
func (p *Player) GetOldActivityRankRewardRequest(req *sp.GetOldActivityRankRewardRequest) (res *sp.GetOldActivityRankRewardResponse) {
	us := p.User
	// 获取用户旧的排名分组id
	oldUserActRank, err := m.GetOldUserActRank(us.Uid.Hex())
	if err != nil {
		log.WithFields(log.Fields{
			"method": "GetOldActivityRankRewardRequest",
			"uid":    us.Uid.Hex(),
			"err":    err,
		}).Error("获取用户旧的排名分组失败")
		res = &sp.GetOldActivityRankRewardResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 当天时间
	nowUnix := utils.TNow().Unix()
	// 当天和记录的活动奖励时间是否一样
	if !utils.IsSameDay(nowUnix, oldUserActRank.RecordTime) {
		log.WithFields(log.Fields{
			"method":     "GetOldActivityRankRewardRequest",
			"uid":        us.Uid.Hex(),
			"nowUnix":    nowUnix,
			"RecordTime": oldUserActRank.RecordTime,
		}).Error("旧的排名和今天时间不是同一天")
		res = &sp.GetOldActivityRankRewardResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}
	uProp, resGoods, boxResList, err := lib.GiveGoodsByKvList("GetOldActivityRankRewardRequest", us, oldUserActRank.ResGoods, p.Send)
	if err != nil {
		fmt.Println("自动领取累计道具失败")
		res = &sp.GetOldActivityRankRewardResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	oldUserActRank.State = 1
	oldUserActRank.Save()

	res = &sp.GetOldActivityRankRewardResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		BoxRes: lib.FormatBoxResSps(boxResList),
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}
